package com.ruoyi.web.controller.meterread;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.WaterMeterDatahistory;
import com.ruoyi.system.domain.WaterYcmeterDatahistory;
import com.ruoyi.system.domain.vo.WaterUserMeterVO;
import com.ruoyi.system.service.IWaterMeterDatahistoryService;
import com.ruoyi.system.service.IWaterUserService;
import com.ruoyi.system.service.IWaterYcmeterDatahistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 抄表数据Controller
 *
 * @author ruoyi
 * @date 2025-09-16
 */
@RestController
@RequestMapping("/meterread/waterData")
public class WaterMeterDatahistoryController extends BaseController
{
    @Autowired
    private IWaterUserService waterUserService;
    @Autowired
    private IWaterMeterDatahistoryService waterMeterDatahistoryService;
    @Autowired
    private IWaterYcmeterDatahistoryService historyService;

    /**
     * 查询所有用户列表
     */
    @PreAuthorize("@ss.hasPermi('meterread:waterData:list')")
    @GetMapping("/list")
    public TableDataInfo list(WaterUserMeterVO userMeterVO)
    {
        startPage();
        List<WaterUserMeterVO> list = waterUserService.selectWaterUserMeterList(userMeterVO);
        return getDataTable(list);
    }

    /**
     * 查询抄表历史数据列表
     */
    @PreAuthorize("@ss.hasPermi('meterread:waterData:hisList')")
    @GetMapping("/hisList")
    public TableDataInfo hisList(WaterMeterDatahistory waterMeterDatahistory)
    {
        startPage();
        if("0".equals(waterMeterDatahistory.getMeterType())){
            WaterYcmeterDatahistory waterYcmeterDatahistory = new WaterYcmeterDatahistory();
            waterYcmeterDatahistory.setMeterNo(waterMeterDatahistory.getMeterNo());
            List<WaterYcmeterDatahistory> list = historyService.selectWaterYcmeterDatahistoryList(waterYcmeterDatahistory);
            return getDataTable(list);
        }else{
            List<WaterMeterDatahistory> list = waterMeterDatahistoryService.selectWaterMeterDatahistoryList(waterMeterDatahistory);
            return getDataTable(list);
        }
    }

    /**
     * 导出抄表数据列表
     */
    @PreAuthorize("@ss.hasPermi('meterread:waterData:export')")
    @Log(title = "抄表数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaterMeterDatahistory waterMeterDatahistory)
    {
        List<WaterMeterDatahistory> list = waterMeterDatahistoryService.selectWaterMeterDatahistoryList(waterMeterDatahistory);
        ExcelUtil<WaterMeterDatahistory> util = new ExcelUtil<WaterMeterDatahistory>(WaterMeterDatahistory.class);
        util.exportExcel(response, list, "抄表数据数据");
    }

    /**
     * 获取抄表数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('meterread:waterData:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(waterMeterDatahistoryService.selectWaterMeterDatahistoryById(id));
    }

    /**
     * 新增抄表数据
     */
    @PreAuthorize("@ss.hasPermi('meterread:waterData:add')")
    @Log(title = "抄表数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WaterMeterDatahistory waterMeterDatahistory)
    {
        return toAjax(waterMeterDatahistoryService.insertWaterMeterDatahistory(waterMeterDatahistory));
    }

    /**
     * 修改抄表数据
     */
    @PreAuthorize("@ss.hasPermi('meterread:waterData:edit')")
    @Log(title = "抄表数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WaterMeterDatahistory waterMeterDatahistory)
    {
        return toAjax(waterMeterDatahistoryService.updateWaterMeterDatahistory(waterMeterDatahistory));
    }

    /**
     * 删除抄表数据
     */
    @PreAuthorize("@ss.hasPermi('meterread:waterData:remove')")
    @Log(title = "抄表数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(waterMeterDatahistoryService.deleteWaterMeterDatahistoryByIds(ids));
    }
}
